package net.dl.ss.dao.ibatis;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import net.dl.ss.dao.AdjudicatorDao;

public class AdjudicatorDaoIbatis extends CommonDaoIbatis implements AdjudicatorDao {

	protected String getTypeAlias() {
        return "Adjudicator";
    }

	/**
     * Retreive all adjudicators from specific table using event id as a criteria.
     *
     * @param id - event id
     * @return the list of adjudicators
     */
    @SuppressWarnings("unchecked")
    public <T> List<T> findByEventId(Serializable id) {
    	try {
            return IbatisUtil.queryList(getClient(), getTypeAlias()+".findByEventId", id);
        } catch(SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    @SuppressWarnings("unchecked")
    public <T> List<T> findByCategoryId(Serializable id) {
    	try {
            return IbatisUtil.queryList(getClient(), getTypeAlias()+".findByCategoryId", id);
        } catch(SQLException e) {
            throw new RuntimeException(e);
        }
    }
    
    /**
     * Ibatis implementation of common interface purposed to save entity if its identifier equals null
     *
     * @param entity entity object which will be made persistent
     * @return same entity populated with newly created identifier
     */
    public <T> T saveCategEntity(final T entity) {
        //((Persistable) entity).setId(nextId());
        try {
            getClient().startTransaction();
            try {
                getClient().insert(getTypeAlias()+".saveEntity", entity);
                getClient().commitTransaction();
            } finally {
                getClient().endTransaction();
            }
        } catch(SQLException e) {
            throw new RuntimeException(e);
        }
        return entity;
    }
    
}
